package com.xxx.exception;


import com.xxx.common.model.RespEnum;
import com.xxx.json.GsonUtil;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ServerException extends RuntimeException{

	private static final long serialVersionUID = -193316907147406110L;

	@Getter
	private Integer code;
	
	@Getter
	private String message;
	
	public ServerException(){}
	
	/**
	 * @param respEnum 枚举信息
	 */
	public ServerException(RespEnum respEnum, Object ... desc){
		super(respEnum.getDesc());
		this.code = respEnum.getCode();
		if (desc != null && desc.length > 0){
			Object[] objArr = desc;
			this.message = String.format(respEnum.getDesc(), objArr);
		}else{
			this.message = respEnum.getDesc();
		}

		if(log.isInfoEnabled()) {
			log.info(respEnum.getCode() + ":" + respEnum.getDesc());
		}
	}


	/**
	 * 
	 * @param respEnum 枚举信息
	 * @param e 异常
	 */
	public ServerException(RespEnum respEnum, Throwable e){
		super(respEnum.getDesc(),e);
		if(log.isErrorEnabled()) {
			log.error(respEnum.getCode() + ":" + respEnum.getDesc() + "|cause:" + e.getCause() + "|message:" + e.getMessage(), e);
		}
		this.code = respEnum.getCode();
		this.message = respEnum.getDesc();
	}
	/**
	 * 
	 * @param respEnum 枚举信息
	 * @param arguments 参数
	 */
	public ServerException(RespEnum respEnum, Object arguments){
		super(respEnum.getDesc());
		if(log.isInfoEnabled()) {
			log.info(respEnum.getCode() + ":" + respEnum.getDesc() + "|arguments:" + GsonUtil.GsonString(arguments));
		}
		this.code = respEnum.getCode();
		if (arguments instanceof String){
			this.message = String.format(respEnum.getDesc(), arguments);
		}else{
			this.message = respEnum.getDesc();
		}

	}
	
	/**
	 * 
	 * @param respEnum 枚举信息
	 * @param e 异常
	 * @param arguments 参数
	 */
	public ServerException(RespEnum respEnum, Object  arguments, Throwable e){
		super(respEnum.getDesc(),e);
		if(log.isErrorEnabled()) {
			log.error(respEnum.getCode() + ":" + respEnum.getDesc() + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + GsonUtil.GsonString(arguments), e);
		}
		this.code = respEnum.getCode();
		this.message = respEnum.getDesc();
	}

	/**
	 *
	 */
	public ServerException(Integer code, String description, Throwable e){
		super(description,e);
		if(log.isErrorEnabled()) {
			log.error(code + ":" + description + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + e);
		}
		this.code = RespEnum.SERVER_ERROR.getCode();
		this.message =  RespEnum.SERVER_ERROR.getDesc();
	}

	/**
	 *
	 */
	public ServerException(String description, Throwable e){
		super(description,e);
		if(log.isErrorEnabled()) {
			log.error(description + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + e);
		}
		this.code = RespEnum.SERVER_ERROR.getCode();
		this.message =  RespEnum.SERVER_ERROR.getDesc();
	}



}
